﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Stock Tape demo</title>
    <link href="../css/quote2.css" rel="stylesheet" type="text/css" />
    <script src="../lib//jquery.js" type="text/javascript"></script>
    <script src="../lib/migratory-jslib.js" type="text/javascript"></script>
    <script src="http://globalquote.morningstar.com/culturefmt/getfmt.ashx?lan=en-US" type="text/javascript"></script>
    <script src="../js/mstar.common.js" type="text/javascript"></script>
    <script src="../js/tickertape.js" type="text/javascript"></script>
</head>
<body>


<div class="rtt-tape" id="rtttickertape1" style="z-index:10000;">
    <div class="rtt-ctn"><ul></ul></div>
    <div class="rtt-addlnk"><a href="#" title="add new ticker">+</a></div>
</div>

<div class="rtt-tape" id="rtttickertape2" style="bottom:26px;_margin-top:-26px;">
    <div class="rtt-ctn"><ul></ul></div>
    <div class="rtt-addlnk"><a href="#" title="add new ticker">+</a></div>
</div>  



<script language="javascript" type="text/javascript">


//fot test
data = [{sourceType:'Stock',serviceName:'NAS',itemName:'YHOO'},
        {sourceType:'Stock',serviceName:'NYSE',itemName:'IBM'},
        {sourceType:'Stock',serviceName:'XOTC',itemName:'XHUA'},
        {sourceType:'Stock',serviceName:'NYSE',itemName:'KO'},
        {sourceType:'Stock',serviceName:'NAS',itemName:'FKFS'},
        {sourceType:'Stock',serviceName:'NYSE',itemName:'PAA'},
        {sourceType:'Stock',serviceName:'NYSE',itemName:'IN'},
        {sourceType:'Stock',serviceName:'NYSE',itemName:'MMM'},
        {sourceType:'Stock',serviceName:'NYSE',itemName:'AAI'},
        {sourceType:'Stock',serviceName:'NYSE',itemName:'XCO'},
        {sourceType:'Stock',serviceName:'NYSE',itemName:'SXE'}
        ];

connectToPusServer();
var rtt1 = new RTQ_TickerTape($('.rtt-tape').eq(0)[0],3,data,'rtt1');
var rtt2 = new RTQ_TickerTape($('.rtt-tape').eq(1)[0],6,[{sourceType:'Index',serviceName:'NAS',itemName:'COMP'},
        {sourceType:'Index',serviceName:'NAS',itemName:'RUT'},
        {sourceType:'Index',serviceName:'NAS',itemName:'SPX'},
        {sourceType:'Index',serviceName:'NAS',itemName:'$INDU'},
        {sourceType:'Index',serviceName:'NAS',itemName:'$MSTAR'},
        {sourceType:'Index',serviceName:'NAS',itemName:'$MFGRS'},
        {sourceType:'Index',serviceName:'NAS',itemName:'$SRVCS'},
        {sourceType:'Index',serviceName:'NAS',itemName:'$INFOS'}
        ],'rtt2');


//push data
 function convertToExchCode(exch)
  {
    if(exch.toUpperCase()=="XSHG") return "136";
    else if(exch.toUpperCase()=="NAS"||exch.toUpperCase()=="NYSE") return "126";
    else if(exch.toUpperCase()=="XSHE") return "137";
    else if(exch.toUpperCase()=="XLON"||exch.toUpperCase()=="LSE") return "151";
    else return "126";
  }
 function connectToPusServer() {
	//if(isConnectedToPushServer) return;
	//jslib.connect(operationHandler, "connectClosure", "http://pushdev.morningstar.com/", updateHandler);
	jslib.connectEntitlement(operationHandler, "connectClosure", "http://push.morningstar.com/", "mstar|Terminal","1","2",updateHandler);
	jslib.addFields(operationHandler, "addFieldsClosure", "LastPrice","ClosePrice","HighPrice","LowPrice"); 
	//isConnectedToPushServer = true;
  }
  
  function subscribeStock(exch,symbol)
 {
  var code = convertToExchCode(exch);
  if(code==null) return;
  jslib.subscribe(operationHandler, "subscribeClosure", "Stock", code, symbol);
  //jslib.addFields(operationHandler, "addFieldsClosure", "ClosePrice");
  //addTicker(symbol);
 }

 function unsubscribeStock(exch,symbol)
 {  var code = convertToExchCode(exch);
  if(code==null) return;
   jslib.unsubscribe(operationHandler, "subscribeClosure", "Stock", code, symbol);
   //removeTicker(symbol);
 }
 
 function operationHandler(closure, eventType, event, body) { 
	if (eventType == jslib.OPERATION_ERROR) {
		var err;
		switch (event) {
			case jslib.ERROR_CONNECTED:
				err = "You are already connected";
				break;
			case jslib.ERROR_NOT_CONNECTED:
				err = "You are not connected";
				break;
			case jslib.ERROR_TIMEOUT:
				err = "Connection timeout";
				break;
			case jslib.ERROR_HTTP:
				err = "HTTP status error. The HTTP status is: " + body;
				break;
			case jslib.ERROR_SERVER:
				err = "Server side error: " + body;
				break;
			case jslib.ERROR_DOMAIN:
				err = "Domain error.";
				break;
			case jslib.ERROR_SERVER_DOWN:
				err = "Server down.";
				break;
		}
		//writeTimeLog(err);
	} else if (eventType == jslib.OPERATION_OK) {
		// everything is ok
	 }
   }
   

   function updateHandler(closure, eventType, event, body) {
	if (eventType == jslib.STREAMING_ERROR) {
		alert("Streaming error=" + event + ", body=" + body);
	} else if (eventType == jslib.MESSAGE_RECEIVED) {
		switch (event) {
		case jslib.MARKET_PRICE_UPDATE:
			updateMarketPrice(body);
			break;
		case jslib.STATUS_UPDATE:
			updateStatus(body);
			break;
		}
	}
  }
  /*this.afterReceiveUpdate = function(obj)
  {
  	if(obj.price!=null&&obj.time !=0)
		{
		  cacheIntradeData(obj.price,obj.time,obj.itemName,"p");
		}
	 if(obj.vol!=null&&obj.time !=0)
	{ cacheIntradeData(obj.vol,obj.time,obj.itemName,"v");}
  };*/
  
  function updateMarketPrice(xmlMessage) {
  
    var obj = {};
  
	var documentBody;
	if (jslib.getBrowser() == "IE") documentBody = xmlMessage.childNodes[1];
	else documentBody = xmlMessage.childNodes[0];
	var itemsReceived = documentBody.childNodes;
	for (var i = 0; i < itemsReceived.length; i++) {
		var sourceType;
		var serviceName;
		var itemName;		
		if (jslib.getBrowser() == "IE") {
			obj.sourceType = itemsReceived[i].childNodes[0].text;
			obj.serviceName = itemsReceived[i].childNodes[1].text;
			obj.itemName = itemsReceived[i].childNodes[2].text;
		} else {
			obj.sourceType = itemsReceived[i].childNodes[0].textContent;
			obj.serviceName = itemsReceived[i].childNodes[1].textContent;
			obj.itemName = itemsReceived[i].childNodes[2].textContent;
		}
		// We received the item named "itemName" with the service "serviceName", from the source type "sourceType"		
		var fieldsForItem = itemsReceived[i].childNodes[3].childNodes;
		var price=null,time=0;
		var vol = null;
		for (var j = 0; j < fieldsForItem.length; j++) {
			var fieldName;
			var value;
			if (jslib.getBrowser() == "IE") {
				fieldName = fieldsForItem[j].childNodes[0].text;
				value = fieldsForItem[j].childNodes[1].text;
			} else {
				fieldName = fieldsForItem[j].childNodes[0].textContent;
				value = fieldsForItem[j].childNodes[1].textContent;
			}
			if(fieldName=="LastPrice") {obj.LastPrice=parseFloat(value);//console.log('lp:'+value)
			}
			if(fieldName=="ClosePrice") {obj.ClosePrice=parseFloat(value);//console.log('cp:'+value)
			}
			if(fieldName=="HighPrice") {obj.HighPrice=parseFloat(value);//console.log('hp:'+value)
			}
			if(fieldName=="LowPrice") {obj.LowPrice=parseFloat(value);//console.log('lwp:'+value)
			}
			//if(fieldName=="Time") time = value;//startTick+parseInt(value)*60000; 
			//if(fieldName=="Volume") vol = parseInt(value);
			}
		}
		rtt1.afterReceiveUpdate(obj);
		rtt2.afterReceiveUpdate(obj);
		//if(price!=null&&time !=0)cacheIntradeData(price,time,itemName,"p");	
		//if(vol!=null&&time !=0)cacheIntradeData(vol,time,itemName,"v");
	}

function updateStatus(xmlMessage) {
	var documentBody;
	if (jslib.getBrowser() == "IE") documentBody = xmlMessage.childNodes[1];
	else documentBody = xmlMessage.childNodes[0];
	var updates = documentBody.childNodes;
	var msg = "";
	for (var i = 0; i < updates.length; i++) {
		var sourceType;
		var serviceName;
		var itemName;
		var itemState;
		var itemStatus;
		if (jslib.getBrowser() == "IE") {
			sourceType = updates[i].childNodes[0].text;
			serviceName = updates[i].childNodes[1].text;
			itemName = updates[i].childNodes[2].text;
			itemState = updates[i].childNodes[3].text;
			itemStatus = updates[i].childNodes[4].text;
		} else {
			sourceType = updates[i].childNodes[0].textContent;
			serviceName = updates[i].childNodes[1].textContent;
			itemName = updates[i].childNodes[2].textContent;
			itemState = updates[i].childNodes[3].textContent;
			itemStatus = updates[i].childNodes[4].textContent;
		}
		// We received a status update for the item "itemName" with service "serviceName", from the source type "sourceType"
		// The status update consists of a state "itemState" and a status "itemStatus"
		// The state is a short indicator of status, possible values are STALE, CLOSED, CLOSED_RECOVER
		// The status is a more detailed description of the state. 
		msg += "Status update, source type='" + sourceType + "', item='" + serviceName + ":" + itemName + "'\nstate=" + itemState + "\nstatus=" + itemStatus + "\n\n";
	}
	alert(msg);
}





</script>

</body>
</html>
